﻿using System.IO;
using Microsoft.AspNetCore.Http;
using System.Net;


namespace Aierp.Utility
{
    public class FileDownHelper
    {
        HttpContext _context;
        public FileDownHelper(HttpContext context)
        {
            _context = context;
        }
        public bool FileExists(string FileName)
        {
            string destFileName = FileName;
            if (System.IO.File.Exists(destFileName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void DownLoadold(string FileName, string name)
        {
            string destFileName = FileName;
            if (System.IO.File.Exists(destFileName))
            {
                FileInfo fi = new FileInfo(destFileName);
                _context.Response.Clear();
                _context.Response.Headers.Clear();
                _context.Response.Headers.Append("Content-Disposition", "attachment;filename=" + WebUtility.UrlEncode(name));
                _context.Response.Headers.Append("Content-Length", fi.Length.ToString());
                _context.Response.ContentType = "application/octet-stream";
                _context.Response.SendFileAsync(destFileName);
                //_context.Response.Flush();
                //_context.Response.End();

            }
        }
    }
}
